# Makefile for LibRHash

include config.mak

HEADERS = algorithms.h byte_order.h plug_openssl.h rhash.h rhash_timing.h rhash_torrent.h aich.h blake2b.h blake2s.h crc32.h ed2k.h edonr.h hex.h md4.h md5.h sha1.h sha256.h sha512.h sha3.h ripemd-160.h gost12.h gost94.h has160.h snefru.h tiger.h tth.h torrent.h ustd.h util.h whirlpool.h
SOURCES = algorithms.c byte_order.c plug_openssl.c rhash.c rhash_timing.c rhash_torrent.c aich.c blake2b.c blake2s.c crc32.c ed2k.c edonr.c hex.c md4.c md5.c sha1.c sha256.c sha512.c sha3.c ripemd-160.c gost12.c gost94.c has160.c snefru.c tiger.c tiger_sbox.c tth.c torrent.c util.c whirlpool.c whirlpool_sbox.c
OBJECTS = $(SOURCES:.c=.o)
LIB_HEADERS = rhash.h rhash_torrent.h
SO_HEADERS = $(LIB_HEADERS) $(LEGACY_HEADERS)
TEST_STATIC = test_static$(EXEC_EXT)
TEST_SHARED = test_shared$(EXEC_EXT)
INSTALL_DATA    = $(INSTALL) -m 644
INSTALL_SHARED  = $(INSTALL) -m 644

all: $(BUILD_TARGETS)
lib-static: $(LIBRHASH_STATIC)
lib-shared: $(LIBRHASH_SHARED)
libs-all: lib-static lib-shared
uninstall-lib: uninstall-lib-shared uninstall-lib-static uninstall-so-link

config.mak:
	echo "Run the ./configure script first" && false

install-lib-static: $(LIBRHASH_STATIC)
	$(INSTALL) -d $(LIBDIR)
	$(INSTALL_DATA) $(LIBRHASH_STATIC) $(LIBDIR)/

install-lib-shared: $(LIBRHASH_SHARED) $(EXTRA_INSTALL_LIBSHARED)
	$(INSTALL) -d $(SO_DIR)
	$(INSTALL_SHARED) $(LIBRHASH_SHARED) $(SO_DIR)/

install-implib:
	$(INSTALL) -d $(LIBDIR)
	$(INSTALL_SHARED) $(LIBRHASH_IMPLIB) $(LIBDIR)/

install-so-link:
	$(INSTALL) -d $(LIBDIR)
	rm -f $(LIBDIR)/$(LIBRHASH_SOLINK)
	ln -s $(LIBRHASH_SHARED) $(LIBDIR)/$(LIBRHASH_SOLINK)

uninstall-lib-static: uninstall-lib-headers
	rm -f $(LIBDIR)/$(LIBRHASH_STATIC)

uninstall-lib-shared: $(EXTRA_UNINSTALL_LIBSHARED)
	rm -f $(SO_DIR)/$(LIBRHASH_SHARED)

uninstall-implib:
	rm -f $(LIBDIR)/$(LIBRHASH_IMPLIB)

uninstall-so-link:
	rm -f $(LIBDIR)/$(LIBRHASH_SOLINK)

install-lib-headers:
	$(INSTALL) -d $(INCDIR)
	$(INSTALL_DATA) $(LIB_HEADERS) $(INCDIR)/

uninstall-lib-headers:
	for f in $(LIB_HEADERS); do rm -f "$(INCDIR)/$$f"; done

# not using GNU make extensions for compatibility with Unix/*BSD make
#%.o: %.c
#	$(CC) -c $(CFLAGS) $< -o $@

# NOTE: dependences were generated by 'gcc -MM -DUSE_OPENSSL *.c'
# we are using plain old makefile style to support BSD make
aich.o: aich.c byte_order.h ustd.h algorithms.h rhash.h aich.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

algorithms.o: algorithms.c byte_order.h ustd.h rhash.h algorithms.h \
 aich.h sha1.h crc32.h ed2k.h md4.h edonr.h gost12.h gost94.h has160.h \
 md5.h ripemd-160.h snefru.h sha256.h sha512.h sha3.h tiger.h torrent.h \
 tth.h whirlpool.h
	$(CC) -c $(CFLAGS) $< -o $@

blake2b.o: blake2b.c blake2b.h ustd.h byte_order.h
	$(CC) -c $(CFLAGS) $< -o $@

blake2s.o: blake2s.c blake2s.h ustd.h byte_order.h
	$(CC) -c $(CFLAGS) $< -o $@

byte_order.o: byte_order.c byte_order.h ustd.h
	$(CC) -c $(CFLAGS) $< -o $@

crc32.o: crc32.c byte_order.h ustd.h crc32.h
	$(CC) -c $(CFLAGS) $< -o $@

ed2k.o: ed2k.c ed2k.h md4.h ustd.h
	$(CC) -c $(CFLAGS) $< -o $@

edonr.o: edonr.c byte_order.h ustd.h edonr.h
	$(CC) -c $(CFLAGS) $< -o $@

gost12.o: gost12.c gost12.h ustd.h byte_order.h
	$(CC) -c $(CFLAGS) $< -o $@

gost94.o: gost94.c gost94.h ustd.h byte_order.h
	$(CC) -c $(CFLAGS) $< -o $@

has160.o: has160.c byte_order.h ustd.h has160.h
	$(CC) -c $(CFLAGS) $< -o $@

hex.o: hex.c hex.h ustd.h
	$(CC) -c $(CFLAGS) $< -o $@

md4.o: md4.c byte_order.h ustd.h md4.h
	$(CC) -c $(CFLAGS) $< -o $@

md5.o: md5.c byte_order.h ustd.h md5.h
	$(CC) -c $(CFLAGS) $< -o $@

plug_openssl.o: plug_openssl.c algorithms.h rhash.h byte_order.h ustd.h \
 plug_openssl.h
	$(CC) -c $(CFLAGS) $< -o $@

rhash.o: rhash.c byte_order.h ustd.h algorithms.h rhash.h torrent.h \
 sha1.h plug_openssl.h util.h hex.h
	$(CC) -c $(CFLAGS) $(VERSION_CFLAGS) $< -o $@

rhash_timing.o: rhash_timing.c byte_order.h ustd.h rhash.h rhash_timing.h
	$(CC) -c $(CFLAGS) $< -o $@

rhash_torrent.o: rhash_torrent.c algorithms.h rhash.h byte_order.h ustd.h \
 torrent.h sha1.h rhash_torrent.h
	$(CC) -c $(CFLAGS) $< -o $@

ripemd-160.o: ripemd-160.c byte_order.h ustd.h ripemd-160.h
	$(CC) -c $(CFLAGS) $< -o $@

sha1.o: sha1.c byte_order.h ustd.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

sha256.o: sha256.c byte_order.h ustd.h sha256.h
	$(CC) -c $(CFLAGS) $< -o $@

sha3.o: sha3.c byte_order.h ustd.h sha3.h
	$(CC) -c $(CFLAGS) $< -o $@

sha512.o: sha512.c byte_order.h ustd.h sha512.h
	$(CC) -c $(CFLAGS) $< -o $@

snefru.o: snefru.c byte_order.h ustd.h snefru.h
	$(CC) -c $(CFLAGS) $< -o $@

test_hashes.o: test_hashes.c byte_order.h ustd.h rhash_timing.h \
 rhash_torrent.h rhash.h test_hashes.h util.h
	$(CC) -c $(CFLAGS) $< -o $@

tiger.o: tiger.c byte_order.h ustd.h tiger.h
	$(CC) -c $(CFLAGS) $< -o $@

tiger_sbox.o: tiger_sbox.c byte_order.h ustd.h
	$(CC) -c $(CFLAGS) $< -o $@

torrent.o: torrent.c byte_order.h ustd.h algorithms.h rhash.h hex.h \
 torrent.h sha1.h
	$(CC) -c $(CFLAGS) $< -o $@

tth.o: tth.c byte_order.h ustd.h tth.h tiger.h
	$(CC) -c $(CFLAGS) $< -o $@

util.o: util.c util.h
	$(CC) -c $(CFLAGS) $< -o $@

whirlpool.o: whirlpool.c byte_order.h ustd.h whirlpool.h
	$(CC) -c $(CFLAGS) $< -o $@

whirlpool_sbox.o: whirlpool_sbox.c byte_order.h ustd.h
	$(CC) -c $(CFLAGS) $< -o $@

# build shared library
$(EXPORTS_FILE): $(SO_HEADERS)
	sed -ne '1s/.*/{ global:/p; s/^RHASH_API.* \(rhash_[a-z0-9_]*\)(.*/  \1;/p; $$s/.*/local: *; };/p' $(SO_HEADERS) > $@

$(LIBRHASH_SOLINK):
	rm -f $(LIBRHASH_SOLINK)
	ln -s $(LIBRHASH_SHARED) $(LIBRHASH_SOLINK)

$(LIBRHASH_SHARED): $(SOURCES) $(EXPORTS_TARGET) $(SOLINK_TARGET)
	$(CC) $(SHARED_CFLAGS) $(VERSION_CFLAGS) $(SOURCES) $(SHARED_LDFLAGS) -o $@

# build static library
$(LIBRHASH_STATIC): $(OBJECTS)
	$(AR) cqs $@ $(OBJECTS)

# test targets
$(TEST_SHARED): $(LIBRHASH_SHARED) test_hashes.o
	$(CC) $(CFLAGS) test_hashes.o $(LIBRHASH_SHARED) $(LDFLAGS) -o $@

$(TEST_STATIC): $(LIBRHASH_STATIC) test_hashes.o
	$(CC) $(CFLAGS) test_hashes.o $(LIBRHASH_STATIC) $(BIN_STATIC_LDFLAGS) -o $@

test: $(TEST_TARGETS)
test-static: $(TEST_STATIC)
	./$(TEST_STATIC)
test-shared: $(TEST_SHARED)
	LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) DYLD_LIBRARY_PATH=.:$(DYLD_LIBRARY_PATH) ./$(TEST_SHARED)

print-info: print-info-$(BUILD_TYPE)
print-info-static: $(TEST_STATIC)
	./$(TEST_STATIC) --info
print-info-shared: $(TEST_SHARED)
	LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) DYLD_LIBRARY_PATH=.:$(DYLD_LIBRARY_PATH) ./$(TEST_SHARED) --info

distclean: clean
	rm -f config.mak

clean:
	rm -f *.o $(LIBRHASH_STATIC) $(LIBRHASH_SHARED) $(TEST_STATIC) $(TEST_SHARED) $(RM_FILES)

.PHONY: all clean distclean install-lib-headers install-lib-shared install-lib-static \
	install-so-link libs-all lib-shared lib-static test test-shared test-static \
	print-info print-info-static print-info-shared uninstall-lib-headers \
	uninstall-lib uninstall-lib-shared uninstall-lib-static uninstall-so-link \
	install-implib uninstall-implib
